package suzf.top.mall.ui.fragment

import android.os.Bundle
import android.support.v7.widget.LinearLayoutManager
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import com.youth.banner.BannerConfig
import com.youth.banner.Transformer
import com.youth.banner.loader.ImageLoader
import kotlinx.android.synthetic.main.fragment_home.*
import me.crosswall.lib.coverflow.CoverFlow
import org.jetbrains.anko.support.v4.startActivity
import suzf.top.base.ext.onClick
import suzf.top.base.ui.fragment.BaseFragment
import suzf.top.base.utils.GlideUtils
import suzf.top.base.widgets.BannerImageLoader
import suzf.top.goodscenter.ui.activity.SearchGoodsActivity
import suzf.top.mall.R
import suzf.top.mall.common.*
import suzf.top.mall.ui.adapter.HomeDiscountAdapter
import suzf.top.mall.ui.adapter.TopicAdapter

/**
 *首页Fragment
 *========================
 * Created with AndroidStudio
 * Author:Mr.Su
 * Date:2019-09-19
 * Time:15:31
 * ========================
 */
class HomeFragment : BaseFragment() {

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        super.onCreateView(inflater, container, savedInstanceState)
        return inflater.inflate(R.layout.fragment_home, null)
    }

    override fun onViewCreated(view: View?, savedInstanceState: Bundle?) {
        initView()
        initBanner()
        initNews()
        initDiscount()
        initTopic()
    }

    private fun initView() {
        mSearchEt.onClick {
            startActivity<SearchGoodsActivity>()
        }
    }

    /*
        初始化banner
     */
    private fun initBanner() {

        mHomeBanner.setImageLoader(BannerImageLoader())
        mHomeBanner.setImages(listOf(HOME_BANNER_ONE, HOME_BANNER_TWO, HOME_TOPIC_THREE, HOME_BANNER_FOUR))
        mHomeBanner.setBannerAnimation(Transformer.Accordion)

        //设置轮播时间
        mHomeBanner.setDelayTime(2000);
        //设置指示器位置（当banner模式中有指示器时）
        mHomeBanner.setIndicatorGravity(BannerConfig.RIGHT);
        //banner设置方法全部调用完毕时最后调用
        mHomeBanner.start();
    }

    /*
        初始化公告
     */
    private fun initNews() {
        mNewsFlipperView.setData(arrayOf("夏日炎炎，第一波福利还是30秒到达战场", "新用户立领1000优惠券"))
    }

    /*
        初始化折扣
     */
    private fun initDiscount() {
        val manager = LinearLayoutManager(context)
        manager.orientation = LinearLayoutManager.HORIZONTAL
        mHomeDiscountRv.layoutManager = manager

        val discountAdapter = HomeDiscountAdapter(activity)
        mHomeDiscountRv.adapter = discountAdapter
        discountAdapter.setData(mutableListOf(HOME_DISCOUNT_ONE, HOME_BANNER_TWO, HOME_BANNER_THREE, HOME_BANNER_FOUR, HOME_DISCOUNT_FIVE))
    }

    /*
       初始化画廊
    */
    private fun initTopic() {
        mTopicPager.adapter = TopicAdapter(context, listOf(HOME_DISCOUNT_ONE, HOME_BANNER_TWO, HOME_BANNER_THREE, HOME_BANNER_FOUR, HOME_DISCOUNT_FIVE))
        mTopicPager.currentItem = 1
        mTopicPager.offscreenPageLimit = 5

        CoverFlow.Builder()
                .with(mTopicPager)
                .scale(0.3f)
                .pagerMargin(-30.0f)
                .spaceSize(0.0f)
                .build()
    }

}